/*
 * @Description: 表格行按钮配置
 * @Author: HuiSir<https://gitee.com/huisir001>
 * @Date: 2025-07-28 21:55:31
 * @LastEditTime: 2025-09-15 18:03:48
 */
import { defineRowBtnsOptions } from "@/components/TablePage/predefined";
import useEditOptions from "./useEditOptions";
import { updateRole, delRole } from "@/api/sys";
import { project } from "@/init/config";
import { fieldMapping } from "@/utils/common";

export default defineRowBtnsOptions(
  (
    getPermTreeList: () => Awaitable<void>,
    setCheckedKeys: (ids: string[]) => void
  ) => ({
    editRequest: updateRole,
    delRequest: delRole,
    editDialogProps: { title: "编辑角色" },
    editFormOptions: useEditOptions(),
    beforeShowEditDialog: async (formData) => {
      // 获取权限树
      await getPermTreeList();
      // 匹配已选
      setCheckedKeys(formData.value.permissionIds || []);
    },
    beforeEditSubmit(formData) {
      // 字段映射
      fieldMapping({
        data: formData.value,
        fieldsMap: project.roleFields,
        reverse: true,
      });
    },
  })
);
